<template>
  <div class="setPermission">
    <el-dialog :visible="isShow" title="分配权限" @close="closeEvent">
      <el-tree
        ref="tree"
        :data="list"
        check-strictly
        show-checkbox
        node-key="id"
        :props="{ label: 'name', children: 'children' }"
      />
      <template #footer>
        <div style="text-align: center">
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button @click="closeEvent">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { sysPermission } from '@/api/permission.js'
import { changeData } from '@/utils/index.js'
import { sysRoleGet, sysRoleAssignPremPut } from '@/api/setting.js'
export default {
  // model:配置v-model自定义指令名
  model: {
    prop: 'isShow',
    event: 'setIsShow'
  },
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      //   show: false,
      list: [],
      permIds: [],
      id: ''
    }
  },
  async created() {
    const res = await sysPermission()
    this.list = changeData(res.data, '0')
    console.log('所有权限数据', res)
  },
  methods: {
    closeEvent() {
      this.$emit('setIsShow', false)
      //   每次关闭弹框就清空选中的数据
      this.$refs.tree.setCheckedKeys([])
    },
    // 根据id获取权限点数据
    async getRuleInfo(id) {
      this.id = id
      const res = await sysRoleGet(id)
      //   保存权限点
      this.permIds = res.data.permIds
      console.log('权限点数据', res)
      //   设置需要选中的节点数据
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    // 点击确定按钮
    async submit() {
      await sysRoleAssignPremPut(this.id, this.$refs.tree.getCheckedKeys())
      this.$message.success('分配权限成功')
      //   关闭弹框 & 清空选中
      this.closeEvent()
    }
  }
}
</script>

<style></style>
